package com.gragra.frameStatistics.tests;
import static org.junit.Assert.*;
import it.unimi.dsi.fastutil.ints.IntSet;
import java.io.BufferedReader;
import java.io.StringReader;
import org.junit.Before;
import org.junit.Test;
import com.gragra.data.Corpus;
import com.gragra.frameStatistics.CoOccurrenceStatistics;
import com.gragra.sampling.sequentialStructure.WordAndTagHolder;
import com.gragra.sequentiaDataGenerator.BaseGenerator;
import com.gragra.sequentiaDataGenerator.CaseLevelAssigningSegmenter;
import com.gragra.sequentiaDataGenerator.PunctuationSplitOffSegmenter;
import com.gragra.util.StringToInt;
public class TestCoOccurrenceStatistics
{
	/**
	 * 
	 */
	private CoOccurrenceStatistics sg;
	@Before
	public void setUp() throws Exception
	{
		StringReader sr = new StringReader("This|0|WordTypeLevel|Noun IS|NONE something.|0|ContextTagLevel|10 \n this|Pronoun is|Verb another|NONE thing.|NONE|WordTypeLevel|Function");
		BufferedReader br = new BufferedReader(sr);
		BaseGenerator bg = new BaseGenerator(new CaseLevelAssigningSegmenter(new PunctuationSplitOffSegmenter("NONE")));
		Corpus<WordAndTagHolder> corp = bg.createCorpus(br);
		sg = new CoOccurrenceStatistics(corp, 3, 1, 1);
	}
	@Test
	public void testGetFollowers()
	{
		IntSet is = sg.getFollowers(StringToInt.ensure("is"));
		assertEquals(is.size(),2);
		assertTrue(is.contains(StringToInt.ensure("something")));
		assertTrue(is.contains(StringToInt.ensure("another")));
		is = sg.getFollowers(StringToInt.ensure("this"));
		assertEquals(is.size(),1);
		assertTrue(is.contains(StringToInt.ensure("is")));
		is = sg.getFollowers(StringToInt.ensure("."));
		assertEquals(is.size(),1);
		assertTrue(is.contains(-1));
	}
	@Test
	public void testGetPrecedents()
	{
		IntSet is = sg.getPrecedents(StringToInt.ensure("is"));
		assertEquals(is.size(),1);
		assertTrue(is.contains(StringToInt.ensure("this")));
		is = sg.getPrecedents(StringToInt.ensure("this"));
		assertEquals(is.size(),1);
		assertTrue(is.contains(-1));
		is = sg.getPrecedents(StringToInt.ensure("."));
		assertEquals(is.size(),2);
		assertTrue(is.contains(StringToInt.ensure("something")));
		assertTrue(is.contains(StringToInt.ensure("thing")));
	}
	@Test
	public void testGetForwardCoCount()
	{
		assertEquals(2,sg.getForwardCoCount(StringToInt.ensure("this"), StringToInt.ensure("is")),0.0000001);
		assertEquals(2,sg.getForwardCoCount(StringToInt.ensure("."), -1),0.0000001);
		assertEquals(1,sg.getForwardCoCount(StringToInt.ensure("is"), StringToInt.ensure("something")),0.0000001);
		assertEquals(1,sg.getForwardCoCount(StringToInt.ensure("is"), StringToInt.ensure("another")),0.0000001);
		assertEquals(0,sg.getForwardCoCount(StringToInt.ensure("is"), StringToInt.ensure("this")),0.0000001);
	}
	@Test
	public void testGetBackwardCoCount()
	{
		assertEquals(2,sg.getBackwardCoCount(StringToInt.ensure("is"), StringToInt.ensure("this")),0.0000001);
		assertEquals(1,sg.getBackwardCoCount(StringToInt.ensure("."), StringToInt.ensure("thing")),0.0000001);
		assertEquals(1,sg.getBackwardCoCount(StringToInt.ensure("."), StringToInt.ensure("something")),0.0000001);
		assertEquals(2,sg.getBackwardCoCount(StringToInt.ensure("this"), -1),0.0000001);
		assertEquals(0,sg.getBackwardCoCount(StringToInt.ensure("."), StringToInt.ensure("this")),0.0000001);
	}
}